package config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

/**
 * Es的Repository这种操作是基于ElasticsearchTemplate的
 *
 * 1.EnableElasticsearchRepositories注解启用对Repository类型的扫描及动态代理的创建，类似于mybatis的MapperScan注解
 * Repository接口不需要指定Repository注解
 * 2.此注解不通过elasticsearchTemplateRef指定具体的template对象时，默认指向名字为elasticsearchTemplate的bean对象
 *
 * es的配置类可选的可以继承AbstractElasticsearchConfiguration，此类有一个抽象的配置rest high level连接客户端的方法
 * 以及注册ElasticsearchRestTemplate的bean方法，AbstractElasticsearchConfiguration有一个
 * 父类ElasticsearchConfigurationSupport，它里面有一些额外的其它配置信息，比如刷新策略。
 */
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.repository.dao",elasticsearchTemplateRef = "abc")
@ComponentScan("com.repository")
public class SpringDataEsRepositoryConfig {



    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http")));

        return client;
    }

    /**
     * bean方法的名字最好是elasticsearchTemplate，这样EnableElasticsearchRepositories
     * 注解就不需要额外再配置elasticsearchTemplateRef
     * @return
     */
    @Bean
    public ElasticsearchRestTemplate abc(){
        ElasticsearchRestTemplate template = new ElasticsearchRestTemplate(restHighLevelClient());
        return template;
    }
}
